package org.mindrive.system.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.mindrive.system.collection.expenses.ApprovalEntity;
import org.mindrive.system.collection.expenses.DetailEntity;
import org.mindrive.system.collection.system.UserEntity;
import org.mindrive.system.service.BaseService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * ScheduledTasks
 *
 * @author mwh
 * @date 2023/10/30
 */
@Component
public class ScheduledTasks extends BaseService {


    /**
     * 用户填报统计
     * @Author mwh
     * @Date 14:00 2023/10/30
     * @Param []
     * @return void
     **/
    @Scheduled(cron = "0 0 1 * * ? ")
    public void fillInStatistics() {
        SimpleDateFormat yearSdf = new SimpleDateFormat("yyyy-MM");
        final String[] newDate = yearSdf.format(new Date()).split("-");
        //查询系统内除管理员外的所有用户
        QueryWrapper<UserEntity> userQuery = new QueryWrapper<>();
        userQuery.ne("USER_ID" , USER_ID_SYSTEM);
        final List<UserEntity> users = userDao.list(userQuery);
        //遍历用户集合,判断对应用户是否新增了当月的填报记录.
        for (UserEntity user : users){
            QueryWrapper<DetailEntity> detailQuery = new QueryWrapper<>();
            detailQuery.eq("CREATOR" , user.getUserId());
            detailQuery.apply("YEAR(CREATE_AT) = "+newDate[0]);
            detailQuery.apply("MONTH(CREATE_AT) = "+newDate[1]);
            List<DetailEntity> detailList = detailDao.list(detailQuery);
            if (detailList.size() == 0){
                //若未填报,用户填报状态变为未填报
                user.setFillOutStatus(NO_STATUS);
                userDao.updateById(user);
            }
        }
    }

}
